// 客户合同mock数据
let contractId = 6; // 用于生成新ID

const mockContracts = [
  {
    id: 1,
    customerCode: 'C001',
    customerName: '北京科技有限公司',
    customerRating: 'A',
    companyType: '有限责任公司',
    salesperson: '张三',
    contractMonthlyFee: 5000,
    contractStatus: '正常',
    serviceItems: '记账代理,税务申报',
    area: '北京市朝阳区',
    paymentPlan: '每月15日',
    receiptStatus: '已收款',
    customerStatus: '正常',
    signDate: '2024-01-15',
    contactPerson: '李经理',
    contactPhone: '13800138001',
    accountant: '王会计',
    monthlyFee: 5000,
    expirationMonths: 12,
    serviceYears: 2.5,
  },
  {
    id: 2,
    customerCode: 'C002',
    customerName: '上海贸易公司',
    customerRating: 'B+',
    companyType: '股份有限公司',
    salesperson: '李四',
    contractMonthlyFee: 3500,
    contractStatus: '正常',
    serviceItems: '记账代理,工商年检',
    area: '上海市浦东新区',
    paymentPlan: '每月10日',
    receiptStatus: '待收款',
    customerStatus: '正常',
    signDate: '2024-02-20',
    contactPerson: '陈总',
    contactPhone: '13900139002',
    accountant: '刘会计',
    monthlyFee: 3500,
    expirationMonths: 24,
    serviceYears: 1.8,
  },
  {
    id: 3,
    customerCode: 'C003',
    customerName: '深圳创新科技',
    customerRating: 'A+',
    companyType: '高新技术企业',
    salesperson: '王五',
    contractMonthlyFee: 8000,
    contractStatus: '正常',
    serviceItems: '记账代理,税务筹划,财务咨询',
    area: '深圳市南山区',
    paymentPlan: '每季度付',
    receiptStatus: '已收款',
    customerStatus: '重点客户',
    signDate: '2023-12-01',
    contactPerson: '张董',
    contactPhone: '13700137003',
    accountant: '赵会计',
    monthlyFee: 8000,
    expirationMonths: 36,
    serviceYears: 3.2,
  },
  {
    id: 4,
    customerCode: 'C004',
    customerName: '广州餐饮连锁',
    customerRating: 'B',
    companyType: '个体工商户',
    salesperson: '赵六',
    contractMonthlyFee: 2800,
    contractStatus: '解约',
    serviceItems: '记账代理',
    area: '广州市天河区',
    paymentPlan: '每月末',
    receiptStatus: '已结清',
    customerStatus: '已解约',
    signDate: '2023-08-15',
    contactPerson: '刘老板',
    contactPhone: '13600136004',
    accountant: '孙会计',
    monthlyFee: 2800,
    expirationMonths: 12,
    serviceYears: 0.8,
  },
  {
    id: 5,
    customerCode: 'C005',
    customerName: '杭州电商平台',
    customerRating: 'A',
    companyType: '有限责任公司',
    salesperson: '钱七',
    contractMonthlyFee: 6500,
    contractStatus: '解约',
    serviceItems: '记账代理,税务申报,审计服务',
    area: '杭州市西湖区',
    paymentPlan: '每月5日',
    receiptStatus: '已结清',
    customerStatus: '已解约',
    signDate: '2023-11-20',
    contactPerson: '周总监',
    contactPhone: '13500135005',
    accountant: '吴会计',
    monthlyFee: 6500,
    expirationMonths: 18,
    serviceYears: 1.2,
  },
];

export default {
  // 获取合同列表
  'GET /api/customer-contracts': (req, res) => {
    const { status, current = 1, pageSize = 10, ...params } = req.query;
    
    // 根据状态过滤
    let filteredContracts = mockContracts;
    if (status === '正常') {
      filteredContracts = mockContracts.filter(contract => contract.contractStatus === '正常');
    } else if (status === '解约') {
      filteredContracts = mockContracts.filter(contract => contract.contractStatus === '解约');
    }
    
    // 搜索过滤
    if (params.customerName) {
      filteredContracts = filteredContracts.filter(contract => 
        contract.customerName.includes(params.customerName)
      );
    }
    if (params.customerCode) {
      filteredContracts = filteredContracts.filter(contract => 
        contract.customerCode.includes(params.customerCode)
      );
    }
    if (params.salesperson) {
      filteredContracts = filteredContracts.filter(contract => 
        contract.salesperson.includes(params.salesperson)
      );
    }
    
    // 分页
    const startIndex = (current - 1) * pageSize;
    const endIndex = startIndex + parseInt(pageSize);
    const pagedData = filteredContracts.slice(startIndex, endIndex);
    
    res.json({
      success: true,
      data: {
        list: pagedData,
        total: filteredContracts.length,
        current: parseInt(current),
        pageSize: parseInt(pageSize),
      },
    });
  },

  // 创建合同
  'POST /api/customer-contracts': (req, res) => {
    const newContract = {
      id: contractId++,
      ...req.body,
      contractStatus: '正常',
      customerStatus: '正常',
    };
    mockContracts.unshift(newContract);
    
    res.json({
      success: true,
      data: newContract,
      message: '合同创建成功',
    });
  },

  // 更新合同
  'PUT /api/customer-contracts/:id': (req, res) => {
    const { id } = req.params;
    const index = mockContracts.findIndex(contract => contract.id == id);
    
    if (index !== -1) {
      mockContracts[index] = { ...mockContracts[index], ...req.body };
      res.json({
        success: true,
        data: mockContracts[index],
        message: '合同更新成功',
      });
    } else {
      res.status(404).json({
        success: false,
        message: '合同不存在',
      });
    }
  },

  // 删除合同
  'DELETE /api/customer-contracts/:id': (req, res) => {
    const { id } = req.params;
    const index = mockContracts.findIndex(contract => contract.id == id);
    
    if (index !== -1) {
      const deletedContract = mockContracts.splice(index, 1)[0];
      res.json({
        success: true,
        data: deletedContract,
        message: '合同删除成功',
      });
    } else {
      res.status(404).json({
        success: false,
        message: '合同不存在',
      });
    }
  },

  // 解约合同
  'POST /api/customer-contracts/:id/terminate': (req, res) => {
    const { id } = req.params;
    const index = mockContracts.findIndex(contract => contract.id == id);
    
    if (index !== -1) {
      mockContracts[index].contractStatus = '解约';
      mockContracts[index].customerStatus = '已解约';
      res.json({
        success: true,
        data: mockContracts[index],
        message: '合同解约成功',
      });
    } else {
      res.status(404).json({
        success: false,
        message: '合同不存在',
      });
    }
  },
}; 